package chapter08.pool;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

@Slf4j
public class BlockingQueue<T> {
    // 1. 任务队列
    private Deque<T> taskQueue=new ArrayDeque<>();

    // 2.锁
    private ReentrantLock lock=new ReentrantLock();

    // 3.生产者条件变量
    private Condition fullWaitSet=lock.newCondition();

    // 4.消费者条件变量
    private Condition emptyWaitSet=lock.newCondition();

    // 5.任务队列容量
    private int capcity;

    public BlockingQueue(int queueCapcity) {

        this.capcity=queueCapcity;
    }


    // 阻塞获取队列任务
    public T take(){
        lock.lock();
        try {
            while (taskQueue.isEmpty()){
                try {
                    emptyWaitSet.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            T t = taskQueue.removeFirst();
            fullWaitSet.signal();
            return t;
        }finally {
            lock.unlock();
        }
    }

    // 带超时的阻塞获取
    public T poll(long timeout,TimeUnit unit) {
        lock.lock();
        try {
            // 定义开始时间
            long nanos=unit.toNanos(timeout);
            while (taskQueue.isEmpty()){
                if (nanos<=0){
                    // 说明等待时间已经超时
//                    throw new TimeoutException("the time of connection is timeout");
                    return null;
                }
                try {
                    nanos = emptyWaitSet.awaitNanos(nanos);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            T t = taskQueue.removeFirst();
            fullWaitSet.signal();
            return t;
        }finally {
            lock.unlock();
        }
    }

    // 添加任务
    public void put(T task){
        lock.lock();
        try {
            while (taskQueue.size()==capcity){
                try {
                    log.debug("task wait join in queue:{}",task);
                    fullWaitSet.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            taskQueue.addLast(task);
            log.debug("task join in queue:{}",task);
            emptyWaitSet.signal();
        }finally {
            lock.unlock();
        }
    }

    // 限时添加任务
    public boolean offer(T task,long timeout,TimeUnit unit){
        lock.lock();
        try {
            long nanos = unit.toNanos(timeout);
            while (taskQueue.size()==capcity){
                try {
                    if (nanos<0){
                        log.warn("task not join in queue:{}",task);
                        return false;
                    }
                    log.debug("task wait join in queue:{}",task);
                    nanos = fullWaitSet.awaitNanos(nanos);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            taskQueue.addLast(task);
            log.debug("task join in queue:{}",task);
            emptyWaitSet.signal();
            return true;
        }finally {
            lock.unlock();
        }
    }

    public void tryPut(ThreadPool.RejectPolicy<T> rejectPolicy, T task) {
        lock.lock();
        try {
            if (taskQueue.size()==capcity){
                rejectPolicy.reject(this,task);
            }else {
                taskQueue.addLast(task);
                log.debug("task join in queue:{}",task);
                emptyWaitSet.signal();
            }
        }finally {
            lock.unlock();
        }
    }
}
